package com.lmx.api.service.impl.inneriml;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.lmx.api.mapper.UserInterfanceInfoMapper;
import com.lmx.api.service.UserInterfanceInfoService;
import com.lmx.commen.model.entity.UserInterfanceInfo;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.ibatis.annotations.Param;

import javax.annotation.Resource;

/**
 * @author lmx
 * @description 针对表【user_interfance_info(用户接口信息表)】的数据库操作Service
 * @createDate 2023-02-05 11:47:47
 */
@DubboService
public class InnerUserInterfanceInfoServiceImpl implements com.lmx.commen.service.InnerUserInterfanceInfoService {
    @Resource
    private UserInterfanceInfoService userInterfanceInfoService;
    @Resource
    private UserInterfanceInfoMapper userInterfanceInfoMapper;

    @Override
    public boolean invokeCount(Long interfanceId, Long usrId) {

//         先判断是否存在
        LambdaQueryWrapper<UserInterfanceInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserInterfanceInfo::getIntrefanceid, interfanceId)
                .eq(UserInterfanceInfo::getUserid, usrId);
        UserInterfanceInfo one = userInterfanceInfoService.getOne(queryWrapper);
        if (one == null) {
            UserInterfanceInfo entity = new UserInterfanceInfo();
            entity.setIntrefanceid(interfanceId);
            entity.setUserid(usrId);
            entity.setTotalnum(100L); // 说明此时为申请调用次数，在测试接口节点，分配100次调用
            userInterfanceInfoService.save(entity);
        }

        int i = userInterfanceInfoMapper.updateTotalnumAndLeftnum(interfanceId, usrId);

        if (i==0){
            return false;
        }else {
            return true;
        }




    }


}
